package 代理模式.Cglib代理;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import 代理模式.静态代理.Player;

import java.lang.reflect.Method;

public class GameProxy implements MethodInterceptor {
    private Player player;

    public void setPlayer(Player player) {
        this.player = player;
    }

    public GameProxy(Player player) {
        this.player = player;
    }

    public Object CglibDaiLi() {
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(player.getClass());
        enhancer.setCallback(this);

        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("开始Cglib代理");
        Object res = method.invoke(player, objects);
        System.out.println("Cglib代理结束");
        return res;
    }
}
